package com.speed.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.speed.entity.Activiter;

public class ActiviterService {

	public Page<Activiter> findActivitersForPage(int pageNumber, int pageSize) {
		return findActivitersForPage(pageNumber, pageSize, null);
	}

	/**
	 * 分頁
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param params
	 *            參數
	 * @return
	 */
	public Page<Activiter> findActivitersForPage(int pageNumber, int pageSize, Map<String, Object> params) {
		StringBuffer sql = new StringBuffer("from " + Activiter.TABLE_NAME);
		if (params == null || params.isEmpty()) {
			return Activiter.MODEL_DAO.paginate(pageNumber, pageSize, "select * ", sql.toString());
		}
		List<Object> values = new ArrayList<Object>();
		Set<Entry<String, Object>> entrySet = params.entrySet();
		sql.append(" where 1=1 ");
		for (Entry<String, Object> e : entrySet) {
			// 以下3个字段均用模糊查询
			if (e.getKey().equals(Activiter.ACTIVITER_MOBILE) || e.getKey().equals(Activiter.ACTIVITER_NAME)
					|| e.getKey().equals(Activiter.ENTERPRISE)) {
				sql.append(" and " + e.getKey() + " like '%" + e.getValue() + "%'");
			} else {
				sql.append(" and " + e.getKey() + " = ? ");
				values.add(e.getValue());
			}
		}
		System.out.println("SQL {  " + sql.toString() + " }");
		Page<Activiter> page = Activiter.MODEL_DAO.paginate(pageNumber, pageSize, "select * ", sql.toString(),
				values.toArray());
		return page;

	}

	/**
	 * 分頁
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param params
	 *            參數
	 * @return
	 */
	public Page<Activiter> findActivitersByPageForOr(int pageNumber, int pageSize, Map<String, Object> params) {
		StringBuffer sql = new StringBuffer("from " + Activiter.TABLE_NAME);
		if (params == null || params.isEmpty()) {
			return Activiter.MODEL_DAO.paginate(pageNumber, pageSize, "select * ", sql.toString());
		}
		List<Object> values = new ArrayList<Object>();
		Set<Entry<String, Object>> entrySet = params.entrySet();
		sql.append(" where 1=1 ");
		for (Entry<String, Object> e : entrySet) {
			// 以下3个字段均用模糊查询
			if (e.getKey().equals(Activiter.ACTIVITER_MOBILE) || e.getKey().equals(Activiter.ACTIVITER_NAME)
					|| e.getKey().equals(Activiter.ENTERPRISE)) {
				sql.append(" or " + e.getKey() + " like '%" + e.getValue() + "%'");
			} else {
				sql.append(" or " + e.getKey() + " = ? ");
				values.add(e.getValue());
			}
		}
		System.out.println("SQL {  " + sql.toString() + " }");
		Page<Activiter> page = Activiter.MODEL_DAO.paginate(pageNumber, pageSize, "select * ", sql.toString(),
				values.toArray());
		return page;

	}

	/**
	 * 搜索所有没有被占用的座位
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param hasSeatId
	 * @param asId
	 * @param content
	 * @return
	 */
	public Page<Activiter> findActivitersHasNoSeatByPage(int pageNumber, int pageSize, List<Long> hasSeatId,
			Integer asId, String content) {
		StringBuffer sql = new StringBuffer("from " + Activiter.TABLE_NAME);
		List<Object> params = new ArrayList<Object>();
		sql.append(" WHERE ");
		sql.append(Activiter.ACTIVITES_ID + " = ? ");
		params.add(asId);

		if (hasSeatId != null || !hasSeatId.isEmpty()) {
			sql.append(" AND ");
			sql.append(Activiter.ID + " NOT IN( ?");
			params.add(hasSeatId.get(0));
			for (int i = 1; i < hasSeatId.size(); i++) {
				sql.append(",?");
				params.add(hasSeatId.get(i));
			}
			sql.append(" )");
		}
		
		if (StrKit.notBlank(content)) {
			sql.append(" AND ( " + Activiter.ACTIVITER_NAME + " like '%" + content + "%' OR "
					+ Activiter.ACTIVITER_MOBILE + " like '%" + content + "%' )");
		}
		
		System.out.println("SQL { " + sql.toString() + " }");
		Page<Activiter> page = Activiter.MODEL_DAO.paginate(pageNumber, pageSize, "select * ", sql.toString(),
				params.toArray());
		return page;
	}

	public Activiter findActiviterByCoditionForOne(Map<String, Object> params, String... fields) {
		StringBuffer sql = new StringBuffer("select ");
		if (fields != null || fields.length != 0) {
			for (int i = 0; i < fields.length; i++) {
				sql.append(fields[i]);
				if (i != fields.length - 1) {
					sql.append(",");
				}
			}
		} else {
			sql.append("*");
		}
		sql.append(" from " + Activiter.TABLE_NAME);
		System.out.println(params);
		if (params.isEmpty()) {
			return Activiter.MODEL_DAO.findFirst(sql.toString());
		}
		Set<Entry<String, Object>> entrySet = params.entrySet();
		sql.append(" where 1=1 ");
		List<Object> values = new ArrayList<Object>();
		for (Entry<String, Object> e : entrySet) {
			sql.append(" and " + e.getKey() + " = ? ");
			values.add(e.getValue());
		}
		System.out.println("SQL {  " + sql.toString() + " }");
		return Activiter.MODEL_DAO.findFirst(sql.toString(), values.toArray());
	}

	public List<Activiter> findActiviterByCoditionForMore(Map<String, Object> params, String... fields) {
		StringBuffer sql = new StringBuffer("select ");
		if (fields != null && fields.length != 0) {
			for (int i = 0; i < fields.length; i++) {
				sql.append(fields[i]);
				if (i != fields.length - 1) {
					sql.append(",");
				}
			}
		} else {
			sql.append("*");
		}
		sql.append(" from " + Activiter.TABLE_NAME);
		System.out.println(params);
		if (params.isEmpty()) {
			return Activiter.MODEL_DAO.find(sql.toString());
		}
		Set<Entry<String, Object>> entrySet = params.entrySet();
		sql.append(" where 1=1 ");
		List<Object> values = new ArrayList<Object>();
		for (Entry<String, Object> e : entrySet) {
			sql.append(" and " + e.getKey() + " = ? ");
			values.add(e.getValue());
		}
		System.out.println("SQL {  " + sql.toString() + " }");
		return Activiter.MODEL_DAO.find(sql.toString(), values.toArray());
	}

	/**
	 * 根据条件查询
	 * 
	 * @return
	 */
	public Activiter getActiviterByCoditionForOne(Map<String, Object> params) {
		StringBuffer sql = new StringBuffer("select * from " + Activiter.TABLE_NAME);
		System.out.println(params);
		if (params.isEmpty()) {
			return Activiter.MODEL_DAO.findFirst(sql.toString());
		}
		Set<Entry<String, Object>> entrySet = params.entrySet();
		sql.append(" where 1=1 ");
		List<Object> values = new ArrayList<Object>();
		for (Entry<String, Object> e : entrySet) {
			sql.append(" and " + e.getKey() + " = ? ");
			values.add(e.getValue());
		}
		System.out.println("SQL {  " + sql.toString() + " }");
		return Activiter.MODEL_DAO.findFirst(sql.toString(), values.toArray());
	}

	/**
	 * 根据条件查询
	 * 
	 * @return
	 */
	public List<Activiter> getActiviterByCoditionForMore(Map<String, Object> params) {
		StringBuffer sql = new StringBuffer("select * from " + Activiter.TABLE_NAME);
		if (params.isEmpty()) {
			return Activiter.MODEL_DAO.find(sql.toString());
		}
		Set<Entry<String, Object>> entrySet = params.entrySet();
		sql.append(" where 1=1 ");
		List<Object> values = new ArrayList<Object>();
		for (Entry<String, Object> e : entrySet) {
			if (e.getKey().equals(Activiter.ACTIVITER_MOBILE)) {
				sql.append(" and " + e.getKey() + " like '%" + e.getValue() + "'");
			} else {
				sql.append(" and " + e.getKey() + " = ? ");
				values.add(e.getValue());
			}
		}
		return Activiter.MODEL_DAO.find(sql.toString(), values.toArray());
	}

	public Page<Activiter> findActivitersByPage(int pageNumber, int pageSize, Boolean b) {

		Page<Activiter> page = Activiter.MODEL_DAO.paginate(pageNumber, pageSize, "select * ",
				"from " + Activiter.TABLE_NAME + " where " + Activiter.SIGN_STATUS + " = ? ", b);

		return page;
	}

	public Page<Activiter> findActivitersByPageAndActivitesId(int pageNumber, int pageSize, Boolean b,
			Integer activitesId) {

		Page<Activiter> page = Activiter.MODEL_DAO
				.paginate(
						pageNumber, pageSize, "select * ", "from " + Activiter.TABLE_NAME + " where "
								+ Activiter.SIGN_STATUS + " = ? AND " + Activiter.ACTIVITES_ID + " = ? ",
						b, activitesId);

		return page;
	}

	/**
	 * 得到活动的总人数
	 * 
	 * @return
	 */
	public Long getTotalActiviters() {
		// 来宾人数 -- 必须要加上 活动的 ID--当前不需要
		Record result = Db
				.findFirst(" select count(" + Activiter.ACTIVITER_MOBILE + ") total from " + Activiter.TABLE_NAME);
		Long totalActiviters = result.get("total");
		return totalActiviters;
	}

	/**
	 * 得到活动的总人数
	 * 
	 * @return
	 */
	public Long getTotalActivitersByActivtitesID(Integer activtitesID) {
		// 来宾人数 -- 必须要加上 活动的 ID--当前不需要
		Record result = Db.findFirst(" select count(" + Activiter.ACTIVITER_MOBILE + ") total from "
				+ Activiter.TABLE_NAME + " where " + Activiter.ACTIVITES_ID + " = ? ", activtitesID);
		Long totalActiviters = result.get("total");
		return totalActiviters;
	}

	/**
	 * 得到未签到或者签到了的人数
	 * 
	 * @return
	 */
	public Long getSignActiviters(Boolean b) {
		// 来宾人数 -- 必须要加上 活动的 ID--当前不需要
		Record result = Db.findFirst(" select count(" + Activiter.ACTIVITER_MOBILE + ")  total from "
				+ Activiter.TABLE_NAME + " where " + Activiter.SIGN_STATUS + " = ? ", b);
		Long signeders = result.get("total");
		return signeders;
	}

	/**
	 * 得到未签到或者签到了的人数
	 * 
	 * @return
	 */
	public Long getSignActivitersByActivtitesID(Boolean b, Integer activtitesID) {
		// 来宾人数 -- 必须要加上 活动的 ID--当前不需要
		Record result = Db
				.findFirst(
						" select count(" + Activiter.ACTIVITER_MOBILE + ")  total from " + Activiter.TABLE_NAME
								+ " where " + Activiter.SIGN_STATUS + " = ? AND " + Activiter.ACTIVITES_ID + " = ? ",
						b, activtitesID);
		Long signeders = result.get("total");
		return signeders;
	}

	public Activiter saveActiviter(Activiter data) {
		Activiter activiter = new Activiter().put(data);
		boolean b = activiter.save();
		return b ? activiter : null;
	}

	public boolean updateActiviter(Activiter data) {

		return data.update();
	}

	/**
	 * 查询对应手机
	 * 
	 * @param mobiles
	 * @param fields
	 * @return
	 */
	public List<Activiter> findActiviterByMobiles(List<String> mobiles, Integer activitesId, String... fields) {
		if (mobiles == null || mobiles.isEmpty()) {
			// 返回一个空列表
			return new ArrayList<Activiter>();
		}
		StringBuffer sql = new StringBuffer("select ");
		if (fields == null || fields.length == 0) {
			sql.append("*");
		} else {
			sql.append(fields[0]);
			for (int i = 1; i < fields.length; i++) {
				sql.append("," + fields[i]);
			}
		}
		List<Object> params = new ArrayList<Object>();
		sql.append(" from " + Activiter.TABLE_NAME + " where ");
		sql.append(" " + Activiter.ACTIVITES_ID + " = ? AND ( ");
		params.add(activitesId);
		sql.append(Activiter.ACTIVITER_MOBILE + " = ? ");
		params.add(mobiles.get(0));
		for (int i = 1; i < mobiles.size(); i++) {
			sql.append(" or " + Activiter.ACTIVITER_MOBILE + " = ? ");
			params.add(mobiles.get(i));
		}
		sql.append(" ) ");
		return Activiter.MODEL_DAO.find(sql.toString(), params.toArray());
	}

	public boolean delActiviter(Activiter activiter) {
		Activiter copyActiviter = new Activiter();
		if (activiter != null) {
			copyActiviter.put(activiter);
			return copyActiviter.delete();
		}
		return false;
	}

	/**
	 * 得到付费人数
	 * 
	 * @return
	 */
	public Long getPayedActiviters(Boolean b, Integer activitesId) {
		// 来宾人数 -- 必须要加上 活动的 ID--当前不需要
		Record result = Db
				.findFirst(
						" select count(" + Activiter.ACTIVITER_MOBILE + ")  total from " + Activiter.TABLE_NAME
								+ " where " + Activiter.IS_PAY + " = ? AND " + Activiter.ACTIVITES_ID + " = ? ",
						b, activitesId);
		Long signeders = result.get("total");
		return signeders;
	}
}
